package com.lanqiao.attendance.handler.TeacherHandler;

import com.lanqiao.attendance.vo.ErrorCode;
import com.lanqiao.attendance.vo.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.dao.*;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;

/**
 * * @Author Pu Yunsheng
 * * @Data 2022/7/10 下午 05:09
 * * @Version 1.0
 **/


/**
 * 抓取教师异常
 */
@Aspect
@Component
public class TeacherExceptionCatchAop {

    @Around("@annotation(com.lanqiao.attendance.handler.TeacherHandler.TeacherExceptionCatch)")
    public Result around(ProceedingJoinPoint joinPoint) throws Throwable {
        Result result = null;
        try{
            result = (Result) joinPoint.proceed();
        }
        //约束遭到破坏
        catch (DataIntegrityViolationException e){
            result = Result.fail(ErrorCode.ACCOUNT_EXIST_ERROR.getCode(),"添加失败,账户已经存在或接收的参数为空");
        }
        //数据库访问失败
        catch (DataAccessResourceFailureException e){
            result = Result.fail(-999,"数据库访问失败！");
        }
        //类型转换失败（java类型和数据库类型不匹配）
        catch (TypeMismatchDataAccessException e){
            result = Result.fail(ErrorCode.PARAMS_ERROR.getCode(), "参数类型错误！");
        }
        //其它
        catch (UncategorizedDataAccessException e){
            result = Result.fail(-999, "SQL异常,未知错误！");
        }
        catch (BadSqlGrammarException e){
            result = Result.fail(-999, "SQL异常,sql语法错误！");
        }
        return result;
    }
}
